<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendance_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	function get_server_time()
	{
		$query = $this->db->query("select CURTIME()")->row_array();
		return $query['CURTIME()'];		
	}
	
	function get_server_date()
	{
		$query = $this->db->query("select CURDATE()")->row_array();
		return $query['CURDATE()'];		
	}
	
	function insert_entry_time($insert)
	{
		//echo "insert"; exit;
		$query = $this->db->get_where('attendance_timings', array(
		'month_id'=>$insert['month_id'],
		'date_id'=>$insert['date_id'],
		'user_id'=>$insert['user_id']
		));
 if($query->num_rows() == 0)
 { 
 $query = $this->db->insert('attendance_timings', $insert);		
		if($query)
		{			
		return "Inserted Successfully";	
		}
		else {
		return "Error in database. Please try Again";
		}
		
 }
 
 else {
	return 0; 
 } 
	}
		
	/*fucntion to check entry time */
	public function check_entry_time($check)
	{
		$query = $this->db->get_where('attendance_timings', array(
		'month_id'=>$check['month_id'],
		'date_id'=>$check['date_id'],
		'user_id'=>$check['user_id']
		));
 if($query->num_rows() > 0){
	$row = $query->row_array();	
	return $row['entry_time'];
}
else{return false;}
}

public function insert_exit_time($time)
	{
	$check = array(
		 'month_id' => $time['month_id'],
		 'date_id' => $time['date_id'],
		 'user_id' => $time['user_id'],
			);
				 
	$data = array(
               'exit_time' => $time['exit_time'],
			   'exit_ip'=>$_SERVER['REMOTE_ADDR']            
            );
			
$query = $this->db->update('attendance_timings', $data, $check);	 
if($query){	
	// after exit time entered update the daily attendance sheets
	
return "Attendance Updated Successfully";	
}
else{
return false;
}
  }
  
 public function check_exit_time($check)
  {
	 $query = $this->db->get_where('attendance_timings', $check,1); 
	 if($query->num_rows() == 1)
	 {
		 $row = $query->row_array();
		 return $row['exit_time'];
	 }
	 else {
		return false; 
	 }
  }
 

  
  public function get_attendance_details()
  {
	  
	  $user_id = array(
	  'user_id' => $this->session->userdata('user_id')
	  );
 $query = $this->db->get_where('daily_attendance_sheet', $user_id)->row_array();
	
	$day = "day_";
	//
	$year = "20".$query['year_id'];
	$month = $query['month_id'];
	
	
	//$date = date('Y-m-d');
	
	$str='[';
	  $num_of_days = cal_days_in_month(CAL_GREGORIAN, $query['month_id'], $year);
	   
	 $final_data=array();
	for($i=1;$i<=$num_of_days;$i++)
	 {
		 if($i<=9)
		 {
			$date_id =  '0'.$i;
		 }
		 else{
			 $date_id = $i;
		 }
		$new_data['id']=$i;
		//if(!empty($query[$day.$date_id]))
		//{
			$new_data['title']= $query[$day.$date_id];
		//}		
		$new_data['start'] = $year.'-'.$month.'-'.$date_id;
		$final_data[]=$new_data;
		 
	 }
	 	$j_data=array();
		foreach($final_data as $day_data){
			$j_data[]=json_encode($day_data);
			}
		$final_str=$str.implode(',',$j_data).']'; 
		return($final_str);   
  }
  
  
   public function insert_daily_attendance_sheet($insert,$sunday)
  {
	  $check = array(
	  'year_id'  =>$insert['year_id'],
	  'month_id' =>	$insert['month_id'],
	  'user_id'  =>$insert['user_id'],	  
	  );
	
	 foreach($sunday as $sundays)
	 {	
		$insert_sunday['day_'.$sundays] = 'sunday';		
	 }
	
	$data = array(
	 array_pop(array_keys($insert))  => end(array_values($insert))
	 );	
	 
	// print_r($data);exit;'
	  $check_query = $this->db->get_where('daily_attendance_sheet', $check); 
	 if($check_query->num_rows() == 1)
	  {
	$query = $this->db->update('daily_attendance_sheet', $data, $check);	
	  }
	  else {	  
		$query = $this->db->insert('daily_attendance_sheet', $insert);    
	 $id = array(
	 'id' => $this->db->insert_id()
	 );
	 	 
	 //inserting all sundays
	 $this->db->update('daily_attendance_sheet', $insert_sunday, $id);	 
	 
	 //inserting all holidays
	 //$this->db->update('daily_attendance_sheet', $insert_holiday, $id);	 
	   
	   //insert into salary table
	   
	   $salary = $this->get_salary_by_id();		
		
	  //insert into monthly table
		$salary_insert= array(
		'year_id'=> $insert['year_id'],
		'month_id'=>$insert['month_id'],
		'user_id'=>$insert['user_id'],
		'payable_salary' => $salary['salary'],
		);
		
		
	  $query = $this->db->insert('monthly_salary_details', $salary_insert);  
	  }
		if($query)
		{			
		return "Inserted Successfully";	
		}
		else {
		return "Error in database. Please try Again";
		}
  }
  
  public function get_salary_by_id($id = null)
  {
	 $user_id = isset($id) ?  $id : $this->session->userdata('user_id');
	  $id = array(
	  'user_id' => $user_id
	  );
	return  $query = $this->db->get_where('salary_account',$id)->row_array();	 
  }
  
  public function add_to_monthly_salary()
  {
	
  }
  
 
  
}
	


